#include <stdio.h>

int permutation(int* a, int n)
{
	int i, j, k, cnt = 0;
	int tmp, flag = 1;
	while(flag)
	{
		for(j = n - 1; j >= 0; j--)
		{printf("%d ", a[j]);}
		printf("\n");

		cnt++;

		for(i = 1; i < n; i++)
		{
			if(a[i] < a[i - 1])
			{break;}
		}

		if(i >= n)
		{break;}

		for(j = i - 1; j >= 0; j--)
		{
			if(a[j] <= a[i])
			{break;}
		}
		// swap
		j++;
		tmp = a[j];
		a[j] = a[i];
		a[i] = tmp;

		// reverse
		k = 0;
		j = i - 1;
		while(k < j)
		{
			tmp = a[k];
			a[k] = a[j];
			a[j] = tmp;
			k++;
			j--;
		}
	}
	return cnt;
}


int main(void)
{
	int a[5] = {5, 4, 3, 2, 1};
	printf("%d\n", permutation(a, 5));
	getchar();
	return 0;
}

